package com.egao.common.component.timer;

import com.egao.common.component.crawler.gongkao.GongKaoProcessor;
import com.egao.common.component.crawler.haitou.HaiTouProcessor;
import com.egao.common.manage.StatisticsServiceImpl;
import com.egao.common.module.cms.entity.Official;
import com.egao.common.module.cms.mapper.OfficialMapper;
import com.egao.common.module.outer.entity.OuterPreach;
import com.egao.common.module.outer.mapper.OuterPreachMapper;
import com.egao.common.module.outer.service.OuterPreachService;
import com.egao.common.module.push.service.PushMsgService;
import com.egao.common.module.system.entity.Year;
import com.egao.common.module.system.service.CrawlerService;
import com.egao.common.module.system.service.YearService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务
 *
 * @author lym
 */
@Component
@Slf4j
public class TimerUtils {

    @Autowired
    private OuterPreachService outerPreachService;
    @Autowired
    private CrawlerService crawlerService;
    @Autowired
    private PushMsgService pushMsgService;
    @Autowired(required = false)
    private OfficialMapper officialMapper;
    @Autowired(required = false)
    private OuterPreachMapper outerPreachMapper;
    @Autowired
    private StatisticsServiceImpl statisticsService;
    @Autowired
    private YearService yearService;

    /**
     * 校外宣讲会爬虫
     * 每天19点00开始
     */
    @Scheduled(cron = "0 00 19 ? * *")
    @Transactional(rollbackFor = Exception.class)
    public void OuterPreachCrawler() {
        List<OuterPreach> list = new ArrayList<>();
        List<OuterPreach> olderList = new ArrayList<>();
        String[] urls = crawlerService.listOuterPreachUrl();

        olderList = outerPreachService.listPreach();

        HaiTouProcessor.start(olderList,list, urls);

        //批量插入
        int limit = 100;
        int listSize = list.size();
        List<OuterPreach> newList = new ArrayList<>();
        for (int i = 0; i < listSize; i++) {
            newList.add(list.get(i));
            if (limit == newList.size() || i == listSize - 1) {
                outerPreachMapper.insertBatch(newList);
                newList.clear();
            }
        }

    }

    /**
     * 公务员信息每天18点开始爬
     */
    @Scheduled(cron = "0 00 18 ? * *")
    @Transactional(rollbackFor = Exception.class)
    public void OfficialCrawler() {
        String[] urls = crawlerService.listOfficialUrl();
        List<Official> list = new ArrayList<>();

        for (String url : urls) {
            GongKaoProcessor gongKaoProcessor = new GongKaoProcessor();
            gongKaoProcessor.start(list, url);
        }

        //批量插入
        int limit = 100;
        int listSize = list.size();
        List<Official> newList = new ArrayList<>();
        for (int i = 0; i < listSize; i++) {
            Official official = list.get(i);

            // 公务员信息标题涵大学但不含中南财经政法大学或涵学院的不保存
            if (1 == official.getCrawlerId()) {
                if (official.getTitle().contains("学院")) {
                    continue;
                }
                if (official.getTitle().contains("大学") && !official.getTitle().contains("中南财经政法大学")) {
                    continue;
                }
            }

            newList.add(official);
            if (limit == newList.size() || i == listSize - 1) {
                officialMapper.insertBatch(newList);
                newList.clear();
            }
        }
    }

//    /**
//     * 校内宣讲会、空中宣讲会推送
//     * 每天17点00开始
//     */
//    @Scheduled(cron = "0 00 17 ? * *")
//    public void pushTomorrowPreach() {
//        log.info("校内宣讲会、空中宣讲会推送");
//        pushMsgService.pushTomorrowPreach();
//    }
//
//    /**
//     * 更新就业状态统计趋势
//     * 每周五10点开始
//     */
//    @Scheduled(cron = "0 0 15 ? * FRI")
//    public void updateStudentSignStatus() {
//        Year year = yearService.getYear();
//        statisticsService.tendStatus(year.getId());
//    }
}
